﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class NewspaperAdmin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.NewspaperAdmin_PageTitle;
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            
            OnlineUser ou = (OnlineUser)Session["User"];

            if (Request.QueryString["NewspaperId"] != null)
                NewspaperIdHiddenField.Value = Request.QueryString["NewspaperId"];
            else
                NewspaperIdHiddenField.Value = ou.Newspaper;
            
            if (!IsPostBack)
            {
                NewspapersDDL.SelectedValue = NewspaperIdHiddenField.Value;
                NewspapersDataSourse.SelectParameters.Add("UserId", ou.UserId);
            }

            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            if (GND.ReadNivdalBoolData("NewsPaperId", "Nivdal_GetNewspaperDir", NewspaperIdHiddenField.Value))
                NewspaperDirPanel.Direction = ContentDirection.RightToLeft;
            else
                NewspaperDirPanel.Direction = ContentDirection.LeftToRight;
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void RedirectToNewspaper(object sender, EventArgs e)
        {
            Response.Redirect("~/NewspaperAdmin.aspx?NewspaperId=" + NewspapersDDL.SelectedValue);
        }

        protected void AddNewRubric(object sender, EventArgs e)
        {
            string RubricName = AddNewRubricTextBox.Text.Trim();
            if (RubricName.Length > 0)
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                GND.ExecNivdalSQL("NewspaperId", "RubricName", "Nivdal_AddNewRubric", NewspaperIdHiddenField.Value, RubricName);
                AddNewRubricTextBox.Text = "";
                RubricsGridview.DataBind();
            }
        }

        protected void UpdateWriter(object sender, RepeaterCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            GridView g = (e.Item.FindControl("RubricsGridView") as GridView);
            foreach (GridViewRow r in g.Rows)
            {
                string RubricId = (r.FindControl("RubricIdHiddenField") as HiddenField).Value;
                bool HasAccess = (r.FindControl("PrivCheckBox") as CheckBox).Checked;
                string UserId = e.CommandArgument.ToString();

                GND.ExecNivdalSQL("UserId", "RubricId", "HasAccess", "Nivdal_UpdateUserRubricAccess", UserId, RubricId, HasAccess);
            }
            g.DataBind();
            (e.Item.FindControl("UpdatedLabel") as Label).Visible = true;
        }

        protected string RoleString(object Role)
        {
            if (Role.ToString() == "3")
                return Resources.Resource.Newspaper_EditorRole;
            else if (Role.ToString() == "2")
                return Resources.Resource.Newspaper_WriterRole;
            return "";
        }

        protected void SendMessageToWriters(object sender, EventArgs e)
        {
            string subject = MessageToWritersTitleTextBox.Text.Trim();
            string Message = GlobalFunctions.LightEncodeHtmlFromText(MessageToWritersBox.Text.Trim());
            string NewsPaperId = NewspaperIdHiddenField.Value;
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            GND.ExecNivdalSQL("Subject", "Message", "NewspaperId",
                "Nivdal_SendMessageToAllNewspaperWriters", subject, Message, NewsPaperId);
            MessageSentLabel.Visible = true;                                
        }

    }



}